<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      text-align: center;
      width: 980px;
      margin: 0 auto;
    }

    .tab {
      margin-bottom: 10px;
    }

    .tab span {
      display: inline-block;
      width: 80px;
      line-height: 50px;
      height: 50px;
      margin-left: 10px;
    }

    .tab span a {
      text-decoration: none;
      color: #000;
    }

    .content {

      width: 500px;
      margin: 0 auto;
      height: 300px;
      border: 1px solid red;
    }

    .tab span .active {
      color: red;
      border-bottom: 3px solid red;
    }

    .item {
      display: none;
    }

    .content .active {
      display: block;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="tab">
      <span><a class="active" href="javascript:;" data-id="0">零食</a></span>
      <span><a href="#" data-id="1">饮料</a></span>
      <span><a href="#" data-id="2">生活用品</a></span>
    </div>
    <div class="content">
      <div class="item active">零食内容</div>
      <div class="item">饮料内容</div>
      <div class="item">生活用品内容</div>
    </div>
  </div>
  <script>
    const tab = document.querySelector('.tab')
    tab.addEventListener('click', function (e) {
      // console.log(e.target);
      if (e.target.tagName === 'A') {
        // console.log('111111');
        document.querySelector('.tab .active').classList.remove('active')
        e.target.classList.add('active')

        const i = +e.target.dataset.id
        document.querySelector('.content .active').classList.remove('active')
        document.querySelector(`.content .item:nth-child(${ i + 1 })`).classList.add('active')
      }
    })

  </script>
</body>

</html>